package top.hbq;

import java.util.Scanner;

/**
 * main
 */
public class Main {
    private static Scanner sc = new Scanner(System.in);
    private static int  player;
    private static Game game;
    public static void main(String[] args) {
        // write your code here
        System.out.println("Welcome to use the Reversi simulation program!");
        while (true){
            System.out.println("Please select menu:");
            System.out.println("\t1.Man-machine combat elementary");
            System.out.println("\t2.Intermediate man-machine battle");
            System.out.println("\t3.Advanced man-machine battle");
            System.out.println("\t4.Machine vs. Elementary");
            System.out.println("\t5.Machine vs. Intermediate");
            System.out.println("\t6.Advanced machine battle");
            System.out.println("\t7.Machine Battle Beginner-Intermediate");
            System.out.println("\t8.Early Machine Battle-Advanced");
            System.out.println("\t9.Machine Battle-Advanced");
            System.out.println("\t10.Help");
            System.out.println("\t0.Exit");
            try {
                String s = sc.nextLine();
                switch (Integer.parseInt(s)){
                    case 0:sc.close();System.exit(0);break;
                    case 1:manMac(1);break;
                    case 2:manMac(1);break;
                    case 3:manMac(3);break;
                    case 4:macMac(1);break;
                    case 5:macMac(2);break;
                    case 6:macMac(3);break;
                    case 7:macMac(1,2);break;
                    case 8:macMac(1,3);break;
                    case 9:macMac(2,3);break;
                    case 10:
                        Help.help();break;
                    default:throw new NumberFormatException();
                }
            }catch (Exception e){
                System.out.println("The data you entered is illegal, please re-enter");
            }
        }
    }
    private static void manMac(int level){
        startPerson();
        game = new Game(level,player==1);
        while (!game.isEnd()){
            game.showGame();
            System.out.println("Please enter your placement position (for example a1):");
            char[] arr = sc.nextLine().toCharArray();
            try {
                game.pressPoint(new MyPoint(arr[1]-49,arr[0]-97));
            }catch (Exception e){
                System.out.println("The data you entered is illegal, please re-enter");
                continue;
            }
        }
        game.Win();
    }
    private static void macMac( int level){
        game = new Game(level,false);
        long currentTime = System.currentTimeMillis();
        game.autoPlay();
        long endTime = System.currentTimeMillis();
        System.out.println("This automatic chess played a total of "+(endTime-currentTime)+"ms");
        game.showGame();
        game.Win();
    }
    private static void macMac(int level1, int level2){
        game = new Game(level1,false);
        long currentTime = System.currentTimeMillis();
        game.autoPlay(level2);
        long endTime = System.currentTimeMillis();
        game.showGame();
        System.out.println("This automatic chess played a total of "+(endTime-currentTime)+"ms");
        game.Win();
    }
    private static final void startPerson(){
        System.out.println("Please choose your role:");
        System.out.println("1.First hand (Black chess)");
        System.out.println("2.Back hand (White chess)");
        while (true){
            try {
                player = Integer.parseInt(sc.nextLine());
                if (player==1||player==2){
                    break;
                }
            }catch (Exception e){
                System.out.println("The data you entered is illegal, please re-enter");
            }
        }
    }
}
